package com.east.controller.fee;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.east.bean.Fee;
import com.east.bean.ReimburseRecords;
import com.east.controller.fee.info.Subject;
import com.east.service.fee.ReimburseService;
import com.east.utils.FileUtils;
import com.east.utils.PageJson;
import com.east.utils.Pagenation;
import com.east.utils.Result;
import com.east.utils.ResultUtil;
import com.east.utils.StringUtils;
import com.east.utils.UploadUtil;
import com.github.pagehelper.Page;

/**
 * 费用管理
 * @author Administrator
 *
 */
@RestController
@RequestMapping("reimburse")
public class ReimburseController {
	@Autowired
	ReimburseService reimburseService;
	/**
	 * 全查
	 * 分页
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("all")
	@RequiresPermissions("reimburse:all")
	public Result findByPage(Pagenation pagenation,String key,String subject,String startTime ,String endTime) {
		Page<ReimburseRecords> findByPage = reimburseService.findByPage(pagenation, key,subject,startTime,endTime);
		Object pageJson = new PageJson<ReimburseRecords>().pageJson(findByPage);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 详情
	 * @param reimburseRecords
	 * @return
	 */
	@GetMapping("findOne")
	@RequiresPermissions("reimburse:findOne")
	public Result findByfindOne(ReimburseRecords reimburseRecords) {
		ReimburseRecords findByProjectId = reimburseService.findByOne(reimburseRecords);
		return ResultUtil.success(findByProjectId);
	}
	/**
	 * 添加
	 * @param reimburseRecords
	 * @return
	 */
	@PostMapping("add")
	@RequiresPermissions("reimburse:add")
	public Result findByAdd(ReimburseRecords reimburseRecords) {
		  boolean saveProject = reimburseService.saveReimburseRecords(reimburseRecords);
		  if(saveProject) {
			  return ResultUtil.success("保存成功");
		  }else {
			  return ResultUtil.success("保存失败");
		  }
		
	}
	/**
	 * 修改
	 * @param reimburseRecords
	 * @return
	 */
	@PostMapping("update")
	@RequiresPermissions("reimburse:update")
	public Result findByUpdate(ReimburseRecords reimburseRecords) {
		  boolean updateProject = reimburseService.updateReimburseRecordse(reimburseRecords);
		  if(updateProject) {
			  return ResultUtil.success("修改成功");
		  }else {
			  return ResultUtil.success("修改失败");
		  }
	}
	/**
	 * 删除
	 * @param reimburseRecords
	 * @return
	 */
	@DeleteMapping("del")
	@RequiresPermissions("reimburse:del")
	public Result findByDel(ReimburseRecords reimburseRecords) {
		 boolean findByProjectId = reimburseService.delReimburseRecords(reimburseRecords);
		 if(findByProjectId) {
			  return ResultUtil.success("删除成功");
		 }else {
				  return ResultUtil.success("删除失败");
		 }
	}
	/**
	 * 导入
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("import")
	@RequiresPermissions("reimburse:import")
	public Result findByimport(MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws Exception {
		String random = StringUtils.random();
		UploadUtil.createDir("E:\\\\mis/upload/reim/"+random);
		file.transferTo(new File("E:\\mis/upload/reim/"+random+"/"+file.getOriginalFilename()));
		List<ReimburseRecords> list = FileUtils.importExcel(
				"E:\\mis/upload/reim/"+random+"/"+file.getOriginalFilename(),
		           0,1,ReimburseRecords.class);	
		String saveListFee = reimburseService.saveListReimburse(list);
		return ResultUtil.success(saveListFee);
		
	}
	/**
	 * 科目汇总
	 * @return
	 */
	@GetMapping("subject")
	@RequiresPermissions("reimburse:subject")
	public Result findsubject(String year,String month) {
		List<Subject> fingBySubject = reimburseService.fingBySubject(year, month);
		return ResultUtil.success(fingBySubject);
	}
}
